//
//  testGetWinners.c
//  dominion
//
//  Created by Doug Mason on 3/8/14.
//  Copyright (c) 2014 Doug Mason. All rights reserved.
//

#include <stdio.h>
#include "testGetWinners.h"
#include "testScoreFor.h"
//TODO: complete implementation of our getWinners test.
int testGetWinners(int players[MAX_PLAYERS],struct gameState* state){
    int numErrors = 0;
    int currentPlayer;
    int compare_player[MAX_PLAYERS];
    int compare_result = 0;
    int high_score = 0;
    int i,j;

    for(i = 0; i < MAX_PLAYERS;i++){
        if(i >= state->numPlayers){
            compare_player[i]= -9999;
        }
        else{
            compare_player[i] = scoreFor(i,state);
        }
    }
    //find raw highest score.
    j = 0;
    for (i = 0; i < MAX_PLAYERS; i++) {
        if(compare_player[i] > compare_player[j]){
            j = i;
        }
    }
    high_score = compare_player[j];
    currentPlayer = whoseTurn(state);
    for (i = currentPlayer+1; i < state->numPlayers; i++) {
        if(compare_player[i] == high_score){
            compare_player[i]+=1;
        }
    }
    //find final highest score.
    j = 0;
    for (i = 0; i < MAX_PLAYERS; i++) {
        if(compare_player[i] > compare_player[j]){
            j = i;
        }
    }
    high_score = compare_player[j];

    compare_result = getWinners(players,state);
    for(i = 0; i < state->numPlayers; i++ ){
        if((compare_player[i]==high_score) && players[i]!=1){
            printf("Failed to determine player %d as a winner with a score of %d\n",i,compare_player[i]);
            numErrors+=1;
        }
    }

    if(numErrors ==0){
        printf("All tests passed for getWinners command!\n");
        return 0;
    }
    else{
        printf("Failed tests for getWinners: %d",numErrors);
        return -1;
    }
}
